<?php

namespace App\Dao;


use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\CreateSmartShortUrlResponseBody\model_;
use App\Exception\MessageException;
use App\Model\ChatRoom;
use Hyperf\Context\ApplicationContext;
use Hyperf\DbConnection\Db;

class ChatRoomDao
{
    private static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = Applicationcontext::getcontainer()->get(self::class);
        }
        return self::$instance;
    }


    /**
     * Note - 获取聊天室列表
     * DateTime 2024/8/30 12:37
     * User Admin
     * @param $type -1:交友 2:点歌 3:游戏 4:其他
     * @param $page
     * @param $pageSize
     * @return array
     */
    public function getRoomList($type, $page, $pageSize)
    {
        if ($type == 0) {
            return ChatRoom::query()
                ->selectRaw('name,room_number,room_id,img_face,level,count_user,notice,label,theme,istop,uid,muted,valid,(base_heat + count_current*10 + room_all_cost + anchor_in_mi*10) as heat')
                ->where('status', 0)
                ->orderByRaw('istop desc, heat desc, ctime asc')
                ->skip(($page - 1) * $pageSize)->take($pageSize)
                ->get()->toArray();
        } else {
            return ChatRoom::query()
                ->selectRaw('name,room_number,room_id,img_face,level,count_user,notice,label,theme,istop,uid,muted,valid,(base_heat + count_current*10 + room_all_cost + anchor_in_mi*10) as heat')
                ->where('status', 0)
                ->where('room_type', $type)
                ->orderBy('istop desc, heat desc, ctime asc')
                ->skip(($page - 1) * $pageSize)->take($pageSize)
                ->get()->toArray();
        }

    }


    /**
     * Note - 获取聊天室信息
     * DateTime 2024/8/30 14:57
     * User Admin
     * @param $roomId
     * @return array|null
     */
    public function showChatRoom($room_number)
    {
        $data = ChatRoom::query()
            ->select(['name', 'room_id', 'room_number', 'img_face', 'level', 'notice', 'label', 'theme', 'uid', 'microphone', 'count_current', 'count_user', 'muted', 'valid',
                Db::raw('(base_heat + count_current*10 + room_all_cost + anchor_in_mi * 10) as heat')
            ])->where('room_number', $room_number)->first();
        if (!$data) {
            throw new MessageException('聊天室不存在！');
        }
        $data = modelToArray($data);
        return $data;
    }

}